package com.codebo.homiematch.common;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class RedisUtils
{

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    // 设置值
    public void setValue(String key, Object value)
    {
        redisTemplate.opsForValue().set(key, value);
    }

    // 获取值
    public Object getValue(String key)
    {
        return redisTemplate.opsForValue().get(key);
    }

    // 删除键
    public Boolean deleteKey(String key)
    {
        return redisTemplate.delete(key);
    }

    // 设置带过期时间的值
    public void setValueWithExpire(String key, Object value, long timeout)
    {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }
}